// feature 特性的意思
import {createSlice} from '@reduxjs/toolkit'

// 创建 slice 对象 (slice 切片)
let slice = createSlice(
  {
    //状态的名字
    name: 'count',
    //状态的初始值
    initialState: 520,
    //reducers  设置 reducer 的功能
    reducers: {
      add: (state, action) => {
        return state + action.payload;
      },
      sub: (state, action) => {
        return state - action.payload;
      }
    }
  }
);

// 获取同步的 action creator
export let {add, sub} = slice.actions;

// 获取 reducer 函数
let reducer = slice.reducer;

//声明异步的 action creator
export let asyncAddAction = (payload) => {
  return dispatch => {
    //编写异步任务
    setTimeout(() => {
      dispatch(add(payload));
    }, 1000)
  }
}

//暴露 reducer 函数
export default reducer;